package com.ujs.code.sortedSquare;

import java.util.Arrays;

/**
 * @author zhangshihao
 * @create 2023-09-17 22:36
 */
public class Solution {
    public static void main(String[] args) {
        int[] nums = {-4, -1, 0, 3, 10};
        int[] result = sortedSquares(nums);
        System.out.println(Arrays.toString(result));
    }

    public static int[] sortedSquares(int[] nums) {
        int left = 0, right = nums.length - 1;
        int[] result = new int[nums.length];
        int k = nums.length - 1;
        while (right >= left) {
            if (nums[right] * nums[right] > nums[left] * nums[left]) {
                result[k--] = nums[right] * nums[right];
                right--;
            } else {
                result[k--] = nums[left] * nums[left];
                left++;
            }
        }
        return result;
    }
}
